import * as echarts from '../ec-canvas/echarts';
const LApp = getApp();

// 防伪
function initSecurityChart(chartObj, dataObj, yArr) {
  var Arr = [];
  if (yArr)
  {
    var length = yArr[1] - yArr[0];
    for(var i=0;i<=length;i++)
    {
      Arr.push(yArr[0]);
      yArr[0]++;
    }
  
  }
  else{
    Arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
  }



  var option = {
    color: ['#3EB05B', '#30B5E1', '#9FAE25', '#F7931E'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 坐标轴指示器，坐标轴触发有效
        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    legend: {
      data: ['微信查询', '网站查询', '电话查询', '短信查询']
    },
    grid: {
      left: 4,
      right: 4,
      bottom: 20,
      top: 40,
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: Arr,
        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        axisTick: { show: false },

        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    series: [
      {
        name: '微信查询',
        type: 'bar',
        label: {
          normal: {
            show: true,
            position: 'outside'
          }
        },
        data: dataObj.WeChat,
      },
      {
        name: '网站查询',
        type: 'bar',
        label: {
          normal: {
            show: true,
            position: 'outside'
          }
        },
        data: dataObj.WebSite,
      },
      {
        name: '电话查询',
        type: 'bar',
        stack: '总量',
        label: {
          normal: {
            show: true,
            position: 'inside'
          }
        },
        data: dataObj.TEL,
      }
      ,
      {
        name: '短信查询',
        type: 'bar',
        stack: '总量',
        label: {
          normal: {
            show: true,
            position: 'outside'
          }
        },
        data: dataObj.SNS,
      }
    ]
  };
  chartObj.setOption(option);
}



// 窜货
function initFleeGoodsChart(chartObj, dataObj) {
  var option = {
    backgroundColor: "#fff",
    color: ["#30B5A7", "#3EB05B", "#ACBD28"],
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['出库', '入库', '退货']
    },
    grid: {
      left: 4,
      right: 10,
      bottom: 20,
      top: 40,
      containLabel: true
    },

    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
    },
    yAxis: {
      x: 'center',
      type: 'value'
    },
    series: [{
      name: '出库',
      type: 'line',
      smooth: true,
      data: dataObj.OutWarehouse
    }, {
      name: '入库',
      type: 'line',
      smooth: true,
      data: dataObj.InWarehouse
    }, {
      name: '退货',
      type: 'line',
      smooth: true,
      data: dataObj.ReturnGoods
    }]
  };
  chartObj.setOption(option);
}


//溯源
function initTraceabilityChart(chartObj, dataObj) {
  var yearArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
  var option = {
    color: ['#3EB05B', '#30B5E1', '#9FAE25', '#F7931E'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 坐标轴指示器，坐标轴触发有效
        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    legend: {
      data: ['溯源']
    },
    grid: {
      left: 4,
      right: 4,
      bottom: 20,
      top: 40,
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: yearArr,
        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        axisTick: { show: false },

        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    series: [
      {
        name: '溯源',
        type: 'bar',
        label: {
          normal: {
            show: true,
            position: 'outside'
          }
        },
        data: dataObj,
      }
    ]
  };
  chartObj.setOption(option);
}

//营销
function initMarketingChart(chartObj, dataObj) {
  var option = {
    backgroundColor: "#fff",
    color: ["#30B5A7", "#ACBD28"],
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['扫码', '领取']
    },
    grid: {
      left: 4,
      right: 10,
      bottom: 20,
      top: 40,
      containLabel: true
    },

    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
    },
    yAxis: {
      x: 'center',
      type: 'value'
    },
    series: [{
      name: '扫码',
      type: 'line',
      smooth: true,
      data: dataObj.ScanCount
    }, {
      name: '领取',
      type: 'line',
      smooth: true,
      data: dataObj.ReceiveCount
    }]
  };
  chartObj.setOption(option);
}

//计算排名百分比

function calcPercent(data){
  if(data&&data.length>0){
    var max=data[0].value*1.5;
    for(var i=0;i<data.length;i++)
    {
      data[i].per = (data[i].value/max).toFixed(2)*100+'%';
    }

    return data;
  }
  else{
    return [];
  }


 
}







Page({
  onShareAppMessage: function () {
  },
  data: {
    security: { lazyLoad: true },
    fleed: { lazyLoad: true },
    traceability: { lazyLoad: true },
    marketing: { lazyLoad: true },
    isModal: false,
    isWordActive: 1,

    security_start_year: '2018',
    security_end_year: '2018',
    security_weChat: '',
    security_webSite: '',
    security_Tel: '',
    security_SNS: '',
    security_real: '',
    security_area: '',

    fleed_start_year: '2018',
    fleed_send: '',
    fleed_real: '',
    fleed_area: '',


    traceability_start_year: '2018',
    traceability_product: '',
    traceability_real: '',
    traceability_area: '',


    marketing_start_year: '2018-01',
    marketing_activitys: '',
    marketing_real: '',
    marketing_area: '',








  },

  onReady() {
    var that = this;
    if (that.data.security_end_year - that.data.security_start_year > 0) {
      LApp._get({
        url: 'nbapi/datanalyse/getantifakedatanalyse',
        data: {
          sYear: that.data.security_start_year,
          eYear: that.data.security_end_year,
        },
        success: function (r) {
          that.initChart('security-dom-chart', initSecurityChart, r.AntifakeYearAnalyse.MonthList);
          that.setData({
            security_weChat: r.AntifakeYearAnalyse.TotalWeChat,
            security_webSite: r.AntifakeYearAnalyse.TotalWeb,
            security_Tel: r.AntifakeYearAnalyse.TotalTel,
            security_SNS: r.AntifakeYearAnalyse.TotalSNS,
            security_real: r.SearchTopTen,
            security_area: calcPercent(r.SearchTopTenPlace),
          })
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
    else if (that.data.security_end_year - that.data.security_start_year == 0) {
      LApp._get({
        url: 'nbapi/datanalyse/getantifakedatanalyse',
        success: function (r) {
          that.initChart('security-dom-chart', initSecurityChart, r.AntifakeYearAnalyse.MonthList);
          that.setData({
            security_weChat: r.AntifakeYearAnalyse.TotalWeChat,
            security_webSite: r.AntifakeYearAnalyse.TotalWeb,
            security_Tel: r.AntifakeYearAnalyse.TotalTel,
            security_SNS: r.AntifakeYearAnalyse.TotalSNS,
            security_real: r.SearchTopTen,
            security_area: calcPercent(r.SearchTopTenPlace),
          })
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
    else {
      wx.showToast({
        title: '结束年份不能小于开始年份',
      })
      setTimeout(function () {
        wx.hideToast()
      }, 3500)
    }


  },
  securityStartYear: function (e) {
    this.setData({
      security_start_year: e.detail.value
    })
  },
  securityEndYear: function (e) {
    this.setData({
      security_end_year: e.detail.value
    })
  },

  fleedStartYear: function (e) {
    this.setData({
      fleed_start_year: e.detail.value
    })
  },

  traceabilityStartYear: function (e) {
    this.setData({
      traceability_start_year: e.detail.value
    })
  },

  marketingStartYear: function (e) {
    this.setData({
      marketing_start_year: e.detail.value
    })
  },


  initChart: function (dom, callback, obj, yearArr) {
    var that = this;
    that.ecComponent = that.selectComponent('#' + dom);
    that.ecComponent.init((canvas, width, height) => { // 获取组件的 canvas、width、height 后的回调函数 
      const chart = echarts.init(canvas, null, { //在这里初始化图表
        width: width,
        height: height
      });
      if (yearArr)
      {
        callback(chart, obj, yearArr);
      }
      else{
        callback(chart, obj);
      }

      that.chart = chart;// 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
    });

  }
  ,

  navigateToFleeGoods: function (e) {

    this.setData({
      isWordActive: e.target.dataset.num,
    })
    var that = this;
    LApp._get({
      url: 'nbapi/datanalyse/getantifleeingdatanalyse',
      success: function (r) {
       
        that.initChart('fleeGoods-dom-chart', initFleeGoodsChart, r.InOrOutOrReturnAnalse);
        that.setData({
          fleed_send: calcPercent(r.OutTopSeven),
          fleed_real: r.SearchTopTen,
          fleed_area: calcPercent(r.AntifleeingTopTenForAntifake),
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },

  navigateToMarketing: function (e) {
    this.setData({
      isWordActive: e.target.dataset.num,
    })
    var that = this;
    LApp._get({
      url: 'nbapi/datanalyse/getactivitydatanalyse',
      success: function (r) {
        that.initChart('marketing-dom-chart', initMarketingChart, r.ActivityAnalyse);
        var marketing_color = ['#30B5A7', '#3EB05B', '#ACBD28', '#EA601A'];
        for (var i = 0; i < r.ActivityProjectAnalyse.length; i++) {
          r.ActivityProjectAnalyse[i].bgcolor = marketing_color[i];
        }

        that.setData({
          marketing_activitys: r.ActivityProjectAnalyse,
          marketing_real: r.AwardTopSeven,
          marketing_area: calcPercent(r.PlaceTopTen),
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })

  },

  navigateToTraceability: function (e) {
    this.setData({
      isWordActive: e.target.dataset.num,
    })
    var that = this;
    LApp._get({
      url: 'nbapi/datanalyse/getoriginsdatanalyse',
      success: function (r) {

        that.initChart('traceability-dom-chart', initTraceabilityChart, r.OriginsMaterial);
        that.setData({
          traceability_product: r.ScanCodeTopSevenForProduct,
          traceability_real: r.SearchTopTen,
          traceability_area: calcPercent(r.SearchTopTenPlace),
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })

  },

  navigateTodataAnalysis: function (e) {
    var that = this;
    this.setData({
      isWordActive: e.target.dataset.num,
    })
    LApp._get({
      url: 'nbapi/datanalyse/getantifakedatanalyse',
      data: {
        sYear: that.data.security_start_year,
        eYear: that.data.security_end_year,
      },
      success: function (r) {

        that.initChart('security-dom-chart', initSecurityChart, r.AntifakeYearAnalyse.MonthList);
        that.setData({
          security_weChat: r.AntifakeYearAnalyse.TotalWeChat,
          security_webSite: r.AntifakeYearAnalyse.TotalWeb,
          security_Tel: r.AntifakeYearAnalyse.TotalTel,
          security_SNS: r.AntifakeYearAnalyse.TotalSNS,
          security_real: r.SearchTopTen,
          security_area: r.SearchTopTenPlace,
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },
  InquireByTime: function (e) {
    var that = this;
    if (e.target.dataset.itemtype == 'security') {
      LApp._get({
        url: 'nbapi/datanalyse/getantifakedatanalyse',
        data: {
          sYear: that.data.security_start_year,
          eYear: that.data.security_end_year,
        },
        success: function (r) {
          var yearArr = [that.data.security_start_year, that.data.security_end_year];
          that.initChart('security-dom-chart', initSecurityChart, r.AntifakeYearAnalyse.MonthList, yearArr);
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
    else if (e.target.dataset.itemtype == 'fleed') {
      LApp._get({
        url: 'nbapi/datanalyse/getantifleeingdatanalyse',
        data: {
          sYear: that.data.fleed_start_year,
        },
        success: function (r) {
          that.initChart('fleeGoods-dom-chart', initFleeGoodsChart, r.InOrOutOrReturnAnalse);
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
    else if (e.target.dataset.itemtype == 'traceability') {
      LApp._get({
        url: 'nbapi/datanalyse/getoriginsdatanalyse',
        data: {
          sYear: that.data.traceability_start_year,
        },
        success: function (r) {
          that.initChart('traceability-dom-chart', initTraceabilityChart, r.OriginsMaterial);
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
    else {
      var time=that.data.marketing_start_year.split('-');
      LApp._get({
        url: 'nbapi/datanalyse/getactivitydatanalyse',
        data: {
          sYear: time[0],
          sMonth: time[1],
        },
        success: function (r) {
          that.initChart('marketing-dom-chart', initMarketingChart, r.ActivityAnalyse);
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
  },



  showModal: function () {
    this.setData({
      isModal: !this.data.isModal,
    })

  },
  hideModal: function () {
    this.setData({
      isModal: false,
    })
  },



});
